<template>
  <div id="home" class="wrapper">
    <nav-bar class="home-nav"><div slot="center">购物街</div> </nav-bar>
    <tab-control ref="tabControl1"
                 :titles="['流行','新款','精选']"
                 @tabClick="tabClick"
                 class="tab-control"
                 v-show="isTabFixed" />
    <scroller class="scroller"
            ref="scroll"
            :probe-type="3"
            @scroll="contentScroll"
            :pull-up-load="true"
            @pullingUp="loadMore">
      <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad"></home-swiper>
      <recommend-view :recommends="recommends" />
      <feature-view/>
      <tab-control ref="tabControl2"
                   :titles="['流行','新款','精选']"
                   @tabClick="tabClick" />
      <goods-list class="goods" :goods="showgoods"/>
    </scroller>
    <back-top @click.native="backClick" v-show="isshow"/>
  </div>
</template>

<script>
  import HomeSwiper from "./childComs/HomeSwiper";
  import RecommendView from "./childComs/RecommendView";
  import FeatureView from "./childComs/FeatureView";

  import NavBar from '@/components/common/navbar/NavBar'
  import TabControl from "@/components/content/tabContorl/TabControl";
  import GoodsList from "@/components/content/goods/GoodsList";
  import Scroller from "@/components/common/scroll/Scroll";


  import { getHomeMultidata, getHomeGoods} from "../../network/home";
  import {itemListenerMinin} from "../../common/mixin";
  import {backTopMixin} from "../../common/mixin";
  import {BACK_POSITION} from "../../common/const";


  export default {
    name: "Home",
    data() {
      return {
        banners: [],
        recommends: [],
        goods: {
          'pop': {page: 0,list: []},
          'new': {page: 0,list: []},
          'sell': {page: 0,list: []},
        },
        currenttype: 'pop',

        tabOffsetTop: 0,
        isTabFixed: false,
        saveY: 0

      }
    },
    components: {
      HomeSwiper,
      RecommendView,
      FeatureView,
      NavBar,
      TabControl,
      GoodsList,
      Scroller
    },
    mixins:[itemListenerMinin,backTopMixin],
    activated() {
      setTimeout(()=>{
        this.$refs.scroll.refresh();
      },2000)


      this.$refs.scroll.scrollTo(0, this.saveY, 0)
    },
    deactivated() {
      //获取滚动的位置
      this.saveY = this.$refs.scroll.getScrollY()
      //当前页面不活跃的时候取消事件总线的监听
      this.$bus.$off('itemImgLoad',this.itemImgListener)
    },
    created() {
      //请求多个数据
      this.getHomeMultidata()
      //请求商品数据
      this.getHomeGoods('pop')
      this.getHomeGoods('new')
      this.getHomeGoods('sell')
    },
    computed: {
      //父传子，把goods.list传给子组件GoodsList
      showgoods(){
        return this.goods[this.currenttype].list
      }
    },
    methods: {
      tabClick(index){
        //正确对应每一个index
        switch (index) {
          case 0:
            this.currenttype = 'pop'
            break
          case 1:
            this.currenttype = 'new'
            break
          case 2:
            this.currenttype = 'sell'
            break
        }
        //实时两者共同绑定currentIndex
        this.$refs.tabControl1.currentIndex = index;
        this.$refs.tabControl2.currentIndex = index;
        // this.currentIndex = index
      },

      //下面是网络请求
      getHomeGoods(type) {
        const page = this.goods[type].page + 1
        getHomeGoods(type, page).then(res => {
          // console.log(res);
          this.goods[type].list.push(...res.data.list)
          this.goods[type].page += 1
          //调用finishPullUp方法可以调用多次上拉加载
          this.$refs.scroll.finishPullUp()
        })
      },
      getHomeMultidata() {
        getHomeMultidata().then(res => {
          this.banners = res.data.banner.list
          this.recommends = res.data.recommend.list
        })
      },
      //监听回到顶部


      contentScroll(position){
        //如果距离小于1000，回顶就隐藏起来
        this.isshow = (-position.y) > BACK_POSITION
        //如果滚动的距离大于tabcontrol与顶部之间的距离，就显示tabcontrol1,否则就不显示
        this.isTabFixed = (-position.y) > this.tabOffsetTop
      },
      //加载更多在当前Type
      loadMore(){
        //引用getHomeGoods方法，传参数this.currenttype
        this.getHomeGoods(this.currenttype)
      },
      swiperImageLoad(){
        //记录好tabcontrol组件距离顶部的距离赋值给一个对象
        this.$refs.scroll.refresh()
        this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop
      },
    },
    updated() {

    },
    mounted() {
      this.tabClick(0)
      //防抖函数
      // const refresh = debounce(this.$refs.scroll.refresh,100)
      // //监听图片有没有加载完成
      this.itemImgListener = ()=> {
        this.$refs.scroll.refresh();
      }

      // this.$bus.$on('itemimgload',this.itemImgListener
      // })

    }
  }
</script>

<style scoped>
  *{touch-action: none;}
  #home{
    height: 100vh;
    position: relative;
  }

.home-nav{
  background-color: #ff8198;
  color: white;
}
  .tab-control{
    position: absolute;
    background-color: #fff;
    z-index: 9;
  }
  .scroller{
    height: calc(100% - 93px);
    /*overflow: hidden;*/
    /*top: 44px;*/
    position: absolute;
    bottom: 49px;
    left: 0px;
    right: 0px;
    overflow: hidden;
  }
</style>
